var gulp = require('gulp');
var gutil = require('gulp-util');
var bower = require('bower');

var concat = require('gulp-concat');
var sass = require('gulp-sass');
var minifyCss = require('gulp-minify-css');
var rename = require('gulp-rename');
var sh = require('shelljs');
var jshint = require('gulp-jshint');
var paths = {
  html:   ['www/**/*.html'],
  sass:   ['src/scss/**/*.scss'],
  script: ['www/js/**/*.js']
};

gulp.task('default', ['sass']);
gulp.task('sass', function(done) {
  gulp.src('src/scss/ionic.app.scss')
    .pipe(sass({
      errLogToConsole: true
    }))
    .pipe(gulp.dest('./www/css/'))
    .on('end', done);
});

gulp.task('lint', function() {
  gulp.src('www/src/js/**/*.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default'));
});
var usemin = require('gulp-usemin');
gulp.task('usemin', function(done) {
  gulp.src('./www/index.html')
    .pipe(usemin({
      css: [ minifyCss(), 'concat' ],
      concat: [ 'concat' ],
      copy:[]
    }))
    .pipe(gulp.dest('./www/build/'));

    gulp.src('./www/ceo.html')
      .pipe(usemin({
        css: [ minifyCss(), 'concat' ],
        concat: [ 'concat' ],
        copy:[]
      }))
      .pipe(gulp.dest('./www/build/'));

    gulp.src('./www/manager.html')
      .pipe(usemin({
        css: [ minifyCss(), 'concat' ],
        concat: [ 'concat' ],
        copy:[]
      }))
      .pipe(gulp.dest('./www/build/'));
    gulp.src('./www/seller.html')
      .pipe(usemin({
        css: [ minifyCss(), 'concat' ],
        concat: [ 'concat' ],
        copy:[]
      }))
      .pipe(gulp.dest('./www/build/'));
    gulp.src('./www/whm.html')
      .pipe(usemin({
        css: [ minifyCss(), 'concat' ],
        concat: [ 'concat' ],
        copy:[]
      }))
    .pipe(gulp.dest('./www/build/'));
    gulp.src('./www/casher.html')
      .pipe(usemin({
        css: [ minifyCss(), 'concat' ],
        concat: [ 'concat' ],
        copy:[]
      }))
      .pipe(gulp.dest('./www/build/'));
});
gulp.task('concatLib', function(done){
  gulp.src([
    "www/src/lib/Chart.min.js",
    "www/src/lib/angular-chart.min.js",
    "www/src/lib/angular-filter.min.js",
  ])
  .pipe(concat('lib.js'))
  .pipe(gulp.dest('www/lib'));
  gulp.src([
    "www/src/lib/particles.min.js",
    "www/src/lib/moment.min.js",
    "www/src/lib/local/zh-cn.js",
    "www/src/lib/angular-moment.min.js",
  ],{ base: 'www/src/lib'})
  .pipe(gulp.dest('www/lib'))
   .on('end', done);
});
gulp.task('watch', function() {
  gulp.watch(paths.sass, ['sass']);
});

gulp.task('install', ['git-check'], function() {
  return bower.commands.install()
    .on('log', function(data) {
      gutil.log('bower', gutil.colors.cyan(data.id), data.message);
    });
});

gulp.task('git-check', function(done) {
  if (!sh.which('git')) {
    console.log(
      '  ' + gutil.colors.red('Git is not installed.'),
      '\n  Git, the version control system, is required to download Ionic.',
      '\n  Download git here:', gutil.colors.cyan('http://git-scm.com/downloads') + '.',
      '\n  Once git is installed, run \'' + gutil.colors.cyan('gulp install') + '\' again.'
    );
    process.exit(1);
  }
  done();
});
